/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pgpproj.sender;

import java.security.KeyPair;
import java.security.PrivateKey;
import java.security.PublicKey;
import javax.crypto.SecretKey;
import pgpproj.pgpframework.authentication.ApplySignatureActor;
import pgpproj.pgpframework.authentication.key.GeneratorKeyPair;
import pgpproj.pgpframework.Message.PGPAuthMessage;
import pgpproj.pgpframework.Message.PGPConfMessage;
import pgpproj.pgpframework.Message.PGPMessage;
import pgpproj.pgpframework.authentication.VerifySignatureActor;
import pgpproj.pgpframework.authentication.algorithm.SignatureEnum;
import pgpproj.pgpframework.crypto.SymetricCipherActor;
import pgpproj.pgpframework.crypto.algorithm.CipherAlgorithmEnum;
import pgpproj.pgpframework.crypto.engine.SymetricCipherEngine;
import pgpproj.pgpframework.hash.SHAHashGenerator;
import pgpproj.pgpframework.util.CryptoUtil;
import pgpproj.receiver.ReceiverActor;

/**
 *
 * @author ufabc
 */
public class SenderActor {

    private PublicKey publicKey;
    private PrivateKey privateKey;
    private PublicKey receiverPubKey;         
            
    public SenderActor() {
        KeyPair par = GeneratorKeyPair.generate("chaves");
        this.privateKey = par.getPrivate();
        this.publicKey = par.getPublic();
    }

    public PGPMessage gerarMensagemPGP(String msg)
    {
    return new PGPMessage((this.gerarMensagemConfidencialidade(msg)),this.gerarMensagemAutenticacao(msg));
    
    }
    
    public PGPAuthMessage gerarMensagemAutenticacao(String msg) {
         String msgHash = generateMsgHash(msg);
        byte[] assinatura = assinaMensagem(msgHash);
        return new PGPAuthMessage(msg, msgHash, assinatura);

    }

    private String generateMsgHash(String msg) {
        SHAHashGenerator hashgen = new SHAHashGenerator();
        return hashgen.generateStringHash(msg);

    }

    private byte[] assinaMensagem(String msg) {
        ApplySignatureActor actor = new ApplySignatureActor();
        return actor.doApllySginature(this.privateKey, msg);
    }
    
    public PGPConfMessage gerarMensagemConfidencialidade(String msg) {

        SymetricCipherEngine cipher =new SymetricCipherEngine();
        SecretKey key = CryptoUtil.generate128SecretKey(); 
        byte[] msgCifrada =  cipher.encryptData(msg.getBytes(),key,CipherAlgorithmEnum.AES);    
        byte[] pubKeyCifrada =  cipher.encryptData(key.getEncoded(),this.receiverPubKey,SignatureEnum.RSA_ECB_PKCS1Padding);   
        return new PGPConfMessage(msgCifrada, pubKeyCifrada);
    }
    
    public PublicKey getPubKey()
    {
        return this.publicKey;
    }
    
    public void setReceiverPubKey(PublicKey key)
    {
    this.receiverPubKey = key;
    }
    
}
